<?php

/**
 * propiedad actions.
 *
 * @package    inmobiliaria
 * @subpackage propiedad
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class propiedadActions extends sfActions
{

public function executeFiltro(sfWebRequest $request)
  {

	  $this->form = new sfForm();	
	  $this->form->setWidgets(array(
    		'tipoprop' => new sfWidgetFormChoice(array('choices' => array('Subject A', 'Subject B', 'Subject C'))),
   		'tipoop' => new sfWidgetFormChoice(array('choices' => array('Subject A', 'Subject B', 'Subject C'))),
//    $this->tipoprop = Doctrine_Core::getTable('TipoPropiedad')

	  ));

  }

  public function executeIndex(sfWebRequest $request)
  {

    $this->form = new sfForm();	
    $this->form->setWidgets(array(
    'tipoprop' => new sfWidgetFormDoctrineChoice(array('model' => 'tipopropiedad', 'multiple' => false, 'add_empty' => true, 'order_by' => array('nombre','asc'))),
    'tipoop' => new sfWidgetFormDoctrineChoice(array('model' => 'tipooperacion', 'multiple' => false, 'add_empty' => true, 'order_by' => array('nombre','asc'))),
    'pais' => new sfWidgetFormDoctrineChoiceChain(array('chain' => array('pais', 'provincia', 'ciudad','zona'))),
    'preciomin'    => new sfWidgetFormInput(),
    'preciomax'    => new sfWidgetFormInput(),
    ));

	  
   $this->form->setValidators(array(
   'pais' => new sfValidatorChoiceChain(array('chain' => array('pais', 'provincia', 'ciudad','zona'))),
  ));


    $preciomin = $request->getParameter('precioMin');
    $preciomax = $request->getParameter('precioMax');
    $tipoprop = $request->getParameter('tipoprop');
    $tipoop = $request->getParameter('tipoop');
    $zona = $request->getParameter('zona');

    $this->propiedads = Doctrine_Core::getTable('Propiedad')
    ->createQuery('a')
    ->where('precio>'.$preciomin.' AND precio<'.$preciomax.' AND tipo_propiedad_id='.$tipoprop.' AND tipo_operacion_id='.$tipoop.' AND zona_id='.$zona)
    ->execute();
    $this->filtro = new PropiedadFormFilter();

    //$this->propiedads = Doctrine_Core::getTable('Propiedad')
      //->createQuery('a')
      //->execute();
  }

  public function executeFilter(sfWebRequest $request)
  {
    $this->filtro = new PropiedadFormFilter();
    $this->consulta = $this->filtro->buildQuery($request->getParameter('query'));
    $this->jobeet_job_list = $this->consulta->execute();
    $this->setTemplate('index');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->propiedad = Doctrine_Core::getTable('Propiedad')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->propiedad);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PropiedadForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PropiedadForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($propiedad = Doctrine_Core::getTable('Propiedad')->find(array($request->getParameter('id'))), sprintf('Object propiedad does not exist (%s).', $request->getParameter('id')));
    $this->form = new PropiedadForm($propiedad);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($propiedad = Doctrine_Core::getTable('Propiedad')->find(array($request->getParameter('id'))), sprintf('Object propiedad does not exist (%s).', $request->getParameter('id')));
    $this->form = new PropiedadForm($propiedad);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($propiedad = Doctrine_Core::getTable('Propiedad')->find(array($request->getParameter('id'))), sprintf('Object propiedad does not exist (%s).', $request->getParameter('id')));
    $propiedad->delete();

    $this->redirect('propiedad/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $propiedad = $form->save();

      $this->redirect('propiedad/edit?id='.$propiedad->getId());
    }
  }
}
